﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Web
{
    public class AuthCode
    {
        public static string GenerateDefault()
        {
            AuthCode auth = new AuthCode();
            return auth.Generate();
        }
        public static string Generate(AuthCodeType codeType, int length)
        {
            AuthCode auth = new AuthCode() { Type = codeType, Length = length };
            return auth.Generate();
        }

        /// <summary>
        /// 验证码长度
        /// </summary>
        public int Length { get; set; } = 5;
        /// <summary>
        /// 验证码类型
        /// </summary>
        public AuthCodeType Type { get; set; } = AuthCodeType.Numeric;//| AuthCodeType.A_Z | AuthCodeType.a_z;

        Random r = new Random();
        /// <summary>
        /// 生成随机验证码
        /// </summary>
        /// <returns></returns>
        public string Generate()
        {
            if (Length < 2)
            {
                throw new Exception("验证码长度必须大于1");
            }

            #region 生成字符集合

            StringBuilder sb = new StringBuilder();// normalchinese;
            if ((this.Type & AuthCodeType.Numeric) == AuthCodeType.Numeric)
            {
                for (byte i = 0; i < 10; i++)
                {
                    sb.Append(i.ToString());
                }
            }
            if ((this.Type & AuthCodeType.A_Z) == AuthCodeType.A_Z)
            {
                for (byte i = (byte)'A'; i <= (byte)'Z'; i++)
                {
                    sb.Append((char)i);
                }
            }
            if ((this.Type & AuthCodeType.a_z) == AuthCodeType.a_z)
            {
                for (byte i = (byte)'a'; i <= (byte)'z'; i++)
                {
                    sb.Append((char)i);
                }
            }
            if ((this.Type & AuthCodeType.Chinese) == AuthCodeType.Chinese)
            {
                sb.Append(normalchinese);
            }
            var txt = sb.ToString();

            #endregion


            char[] cs = new char[Length];
            for (int i = 0; i < Length; i++)
            {
                cs[i] = getOne(txt);
            }
            return new string(cs);
        }
        private const string normalchinese = @"的一了是我不在人们有来他这上着个地到大里说就去子得也和那要下看天时过出小么起你都把好还多没为又可家学只以主会样年想生同老中十从自面前头道它后然走很像见两用她国动进成回什边作对开而己些现山民候经发工向事命给长水几义三声于高手知理眼志点心战二问但身方实吃做叫当住听革打呢真全才四已所敌之最光产情路分总条白话东席次亲如被花口放儿常气五第使写军吧文运再果怎定许快明行因别飞外树物活部门无往船望新带队先力完却站代员机更九您每风级跟笑啊孩万少直意夜比阶连车重便斗马哪化太指变社似士者干石满日决百原拿群究各六本思解立河村八难早论吗根共让相研今其书坐接应关信觉步反处记将千找争领或师结块跑谁草越字加脚紧爱等习阵怕月青半火法题建赶位唱海七女任件感准张团屋离色脸片科倒睛利世刚且由送切星导晚表够整认响雪流未场该并底深刻平伟忙提确近亮轻讲农古黑告界拉名呀土清阳照办史改历转画造嘴此治北必服雨穿内识验传业菜爬睡兴形量咱观苦体众通冲合破友度术饭公旁房极南枪读沙岁线野坚空收算至政城劳落钱特围弟胜教热展包歌类渐强数乡呼性音答哥际旧神座章帮啦受系令跳非何牛取入岸敢掉忽种装顶急林停息句区衣般报叶压慢叔背细";

        char getOne(string txt)
        {

            var index = r.Next(0, txt.Length);

            return txt[index];
        }
    }
    [Flags]
    public enum AuthCodeType
    {
        /// <summary>
        /// 数字
        /// </summary>
        Numeric = 1,
        /// <summary>
        /// 大写字母
        /// </summary>
        A_Z = 2,
        /// <summary>
        /// 小写字母
        /// </summary>
        a_z = 4,
        /// <summary>
        /// 常见汉字
        /// </summary>
        Chinese = 8
    }
}
